<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Attack extends Finecms_Controller {

	public function index()
	{


        $page = (int)$this->get('page') ? (int)$this->get('page') : 1;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $logsdir  = WEBPATH . 'cache' . DIRECTORY_SEPARATOR . 'attack' . DIRECTORY_SEPARATOR;
        $file_list = $this->instance('file_list');
        $filedata = $file_list::get_file_list($logsdir);
        $data   = array();
        $ip = IS_POST ? $this->post('kw') : $this->get('ip');
        if ($filedata) {
            $filedata  = array_reverse($filedata);
            foreach ($filedata as $file) {
                if (substr($file, -4) == '.log') {
                    $fdata = file_get_contents($logsdir . $file);
                    $fdata = explode(PHP_EOL, $fdata);
                    foreach ($fdata as $v) {
                        $t = unserialize($v);
                        if ($t && is_array($t)) {
                            if ($ip) {
                                if ($t['ip'] == $ip) $data[] = $t;
                            } else {
                                $data[] = $t;
                            }
                        }
                    }
                }
            }
        }
        $total    = count($data);
        $pagesize = isset($this->site['SITE_ADMIN_PAGESIZE']) && $this->site['SITE_ADMIN_PAGESIZE'] ? $this->site['SITE_ADMIN_PAGESIZE'] : 8;
        $list     = array();
        $offset   = ($page - 1) * $pagesize;
        foreach ($data as $i => $t) {
            if ($i >= $offset && $i < $offset + $pagesize) {
                $list[] = $t;
            }
        }
        $pagelist = $pagelist->total($total)->url(dr_url('attack/index', array('page' => '{page}', 'ip' => $ip)))->num($pagesize)->page($page)->output();
        $this->template->assign(array(
            'ip' => $ip,
            'list'     => $list,
            'pagelist' => $pagelist
        ));
        $this->template->display('attack.html');
	}
}
